package com.ssm.collection.admin.base;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ssm.collection.core.common.dao.DynamicSqlParameter;
import com.ssm.collection.core.common.util.SSMUtils;
import com.ssm.collection.core.PropConfig;

public abstract class AbstractWebController {

	public static final int PAGE_SIZE = PropConfig.getParameterAsInt("ADMIN_PAGE_SIZE", 10);
	protected final Logger logger;

	public AbstractWebController() {
		logger = LoggerFactory.getLogger(getClass());
	}

	public int getParamOffset(Integer offset) {
		if (offset == null) {
			offset = 0;
		}
		return offset;
	}

	protected int getParamLimit(Integer limit) {

		if (limit == null) {
			limit = PAGE_SIZE;
		}
		return limit;
	}

	public DynamicSqlParameter getAllParameter(HttpServletRequest request) {
		DynamicSqlParameter params = new DynamicSqlParameter();
		Enumeration<String> enu = request.getParameterNames();
		while (enu.hasMoreElements()) {
			String paraName = (String) enu.nextElement();
			String paramValue = request.getParameter(paraName);
			params.put(paraName, paramValue);
		}
		logger.debug("本次请求的参数：" + SSMUtils.objectConvertJson(params));
		return params;
	}

	public void removeEmptyString(DynamicSqlParameter params) {
		Set<Entry<String, Object>> entrySet = params.entrySet();
		Iterator<Entry<String, Object>> iterator = entrySet.iterator();
		while (iterator.hasNext()) {
			Entry<String, Object> next = iterator.next();
			if (next.getValue() != null) {
				if (StringUtils.isBlank(next.getValue().toString())) {
					iterator.remove();
				}
			}
		}
		logger.debug("移除空字符串后的参数：" + SSMUtils.objectConvertJson(params));
	}
}
